/**
 * @param {string} s
 * @return {number}
1. 首先这里面最长的也只有两个罗马字符，所以我的思路是通过字符来判断
2. 写一个循环一直执行
3. 因为罗马字符最 长是两个字符，所以我们先判断两个再判断一个
4. 最后存储结果代码


 */
var romanToInt = function(num) {
    let values = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
    let romaStr = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
    //检测存在的值
    function exist(arr, val) {
      return arr.some((romaVal) => {
        return val === romaVal;
      });
    }
    let initNum = num
    let remainStr = num
    let res = 0;
      //判断前面两个字符存不存在
    while(remainStr.length > 0){
      if(exist(romaStr,remainStr.slice(0,2)) === true){
        initNum = remainStr.slice(0,2)
        remainStr = remainStr.substr(2)
        res += values[romaStr.indexOf(initNum)]
      }else {
        initNum = remainStr.slice(0,1)
        remainStr = remainStr.substr(1)
        res += values[romaStr.indexOf(initNum)]
      }
    }
    return res;
    // console.log(res)
};